package com.weixin.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.weixin.base.dao.UserDao;
import com.weixin.base.service.UserService;
import com.weixin.base.util.PropertiesFile;
import com.weixin.base.util.Tools;
import com.weixin.entity.BgUser;
import com.weixin.entity.TbWxUser;
import com.weixin.entity.TbWxUserUrl;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private PropertiesFile propertiesFile;
	
	public List<Map<String,Object>> getUserListbytype(int ntype){
		return userDao.getUserListbytype(ntype);
	}
	public BgUser login(String username,String password){
		return userDao.login(username, password);
	}
	
	public List<Map<String, Object>> getNavTreeByUser(int userID){
		return userDao.getNavTreeByUser(userID);
	}
	public Page findUserByPage(PageRequest<Map> pageRequest){
		return userDao.findUserByPage(pageRequest);
	}
	
	public int selectIsWrite(int oUserId,String sPageName){
		return userDao.selectIsWrite(oUserId, sPageName);
	}
	
	public int isExit(BgUser entity){
		return userDao.isExit(entity);
	}
	
	public void save(BgUser entity){
		userDao.save(entity);
	}
	
	public int isExit2(BgUser entity){
		return userDao.isExit2(entity);
	}
	
	public void update(BgUser entity){
		userDao.update(entity);
	}
	
	public void deleteById(int sId){
		userDao.delById(sId);
	}
	public void saveUserRelRight(String sRoles,int nuserId){
		userDao.saveUserRelRight(sRoles, nuserId);
	}
	
	public Map<String,Object>  selectUserRelRight(int nuserId){
		return userDao.selectUserRelRight(nuserId);
	}
	
	public BgUser getByLogin(String userName,String passWord){
		return userDao.getByLogin(userName,passWord);
	}
	
	public int updatePwd(String userName,String pwdmd5){
		return userDao.updatePwd(userName,pwdmd5);
	}
	
	public int monitorserversql(){
		return userDao.monitorserversql();
	}
	
	/**
	 * 查询微信号信息
	 */
	public TbWxUser selectWxUser(int nuserId){
		return userDao.selectWxUser(nuserId);
	}
	/**
	 * 添加微信号信息
	 * 同时添加用户首页链接地址表
	 */
	public int addWxUser(TbWxUser tbWxUser){
		TbWxUserUrl tbWxUserUrl=new TbWxUserUrl();
		tbWxUserUrl.setDCreateTime(new Date());
		tbWxUserUrl.setNIsValid(1);
		tbWxUserUrl.setNType(1);
		tbWxUserUrl.setNUserId(tbWxUser.getNUserId());
		tbWxUserUrl.setSName(Tools.tpMainName);
		tbWxUserUrl.setSUrl(propertiesFile.getTpMainUrl()+"?userId="+tbWxUser.getNUserId());
		if(userDao.addWxUserUrl(tbWxUserUrl)>0){
			tbWxUser.setNTemplateId(1);
			return userDao.addWxUser(tbWxUser);
		}else{
			return 0;
		}
	}
	/**
	 * 修改微信号信息
	 */
	public int upWxUser(TbWxUser tbWxUser){
		return userDao.upWxUser(tbWxUser);
	}
}
